package cz.muni.fi.pa165.library.web_layer.adminPages.Reader;

import cz.muni.fi.pa165.library.api.ReaderService;
import cz.muni.fi.pa165.library.api.ReaderTO;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.Template;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author Matúš Abaffy
 */
public class EditReader extends Template {

    private static final long serialVersionUID = 1L;
    
    private ReaderTO reader;

    public EditReader(final PageParameters parameters) {
        super(parameters);
        ReaderService readerService = (ReaderService) ApplicationContextProvider.getApplicationContext().getBean("readerService");
        StringValue readerId = parameters.get("readerId");
        if (readerId.isEmpty()) {
            reader = new ReaderTO();
            add(new Label("title", new Model("Create reader")));
        } else {
            reader = readerService.findReaderById(readerId.toLong());
            add(new Label("title", new Model("Edit reader")));
        }

        Form<?> form = new Form("form") {
            @Override
            protected void onSubmit() {
                ReaderService readerService = (ReaderService) ApplicationContextProvider.getApplicationContext().getBean("readerService");
                if (reader.getId() == null) {
                    readerService.createReader(reader);
                } else {
                    readerService.updateReader(reader);
                }
                setResponsePage(ShowAllReader.class);
            }
        };
        add(form);

        form.add(new TextField("firstName", new PropertyModel<String>(reader, "firstName")).setRequired(true));
        form.add(new TextField("surname", new PropertyModel<String>(reader, "surname")).setRequired(true));
        form.add(new TextField("birthNumber", new PropertyModel<String>(reader, "birthNumber")).setRequired(true));
        form.add(new TextField("address", new PropertyModel<String>(reader, "address")).setRequired(true));
        form.add(new TextField("email", new PropertyModel<String>(reader, "email")).setRequired(true));
        form.add(new TextField("telephoneNumber", new PropertyModel<String>(reader, "telephoneNumber")));
        form.add(new PasswordTextField("password", new PropertyModel<String>(reader, "password")));
        
        BookmarkablePageLink readers = new BookmarkablePageLink<>(
                "list", ShowAllReader.class);
        add(readers);
    }
}